clear
clc

load('../../data/CTU_Normal_vs_Adw_Drp_Rtk_Susp.mat')

ind_norm = find(Y==0);
ind_adw = find(Y==1);
ind_drp = find(Y==2);
ind_rtk = find(Y==3);
ind_susp = find(Y==4);
inds = {ind_norm, ind_adw, ind_drp, ind_rtk, ind_susp};

%% Packet: fig 1
fig1 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% inbound bytes
subplot_pib = subplot(1,4,1);

pib = X{1}(1,:)';
splits = [0, 2000, 4000, 6000, 1/eps];
res_pib = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(pib(inds{ii})>=splits(is) & pib(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_pib(is,:) = tmp;
end

bar(res_pib);
title('\fontsize{14}{Inbound Bytes}');
ylabel('Ratio')
xlabel('Number of Byte')
set(subplot_pib, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2k', '2k ~ 4k', '4k ~ 6k', '\geq 6k'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% inbound packets
subplot_pip = subplot(1,4,2);

pip = X{1}(2,:)';
splits = [0, 2, 4, 6, 1/eps];
res_pip = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(pip(inds{ii})>=splits(is) & pip(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_pip(is,:) = tmp;
end

bar(res_pip);
title('\fontsize{14}{Inbound Packets}');
ylabel('Ratio')
xlabel('Number of Packet')
set(subplot_pip, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2', '2 ~ 4', '4 ~ 6', '\geq 6'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square


% outbound bytes
subplot_pob = subplot(1,4,3);

pob = X{1}(3,:)';
splits = [0, 500, 1000, 1500, 1/eps];
res_pob = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(pob(inds{ii})>=splits(is) & pob(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_pob(is,:) = tmp;
end

bar(res_pob);
title('\fontsize{14}{Outbound Bytes}');
ylabel('Ratio')
xlabel('Number of Byte')
set(subplot_pob, 'XTick', [1,2,3,4], 'XTickLabel', {'<0.5k', '0.5k~1k', '1k~1.5k', '\geq1.5k'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% outbound packets
subplot_pop = subplot(1,4,4);

pop = X{1}(4,:)';
splits = [0, 2, 4, 6, 1/eps];
res_pop = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(pop(inds{ii})>=splits(is) & pop(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_pop(is,:) = tmp;
end

bar(res_pop);
title('\fontsize{14}{Outbound Packets}');
ylabel('Ratio')
xlabel('Number of Packet')
set(subplot_pop, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2', '2 ~ 4', '4 ~ 6', '\geq 6'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square


%% Packet: fig 2
fig2 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% duration
subplot_pd = subplot(1,4,1);

pd = X{1}(5,:)';
splits = [0, 1, 2, 3, 1/eps];
res_pd = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(pd(inds{ii})>=splits(is) & pd(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_pd(is,:) = tmp;
end

bar(res_pd);
title('\fontsize{14}{Duration}');
ylabel('Ratio')
xlabel('Time (s)')
set(subplot_pd, 'XTick', [1,2,3,4], 'XTickLabel', {'< 1', '2 ~ 3', '3 ~ 4', '\geq 4'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% SPL
subplot_pspl = subplot('position',[.337  .307  .2565  .42]);
% axes('position',[.1  .1  .8  .6])

pspl = X{1}(6:16,:)';
res_pspl = zeros(11, length(inds));
for is=1:11
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = mean(pspl(inds{ii},is));
    end
    res_pspl(is,:) = tmp;
end

bar(res_pspl);
title('\fontsize{14}{SPL}');
ylabel('Number of Packet')
xlabel('Index of Bin')
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square


% SPL
subplot_pspt = subplot('position',[.649  .307  .2565  .42]);
% axes('position',[.1  .1  .8  .6])

pspt = X{1}(17:27,:)';
res_pspt = zeros(11, length(inds));
for is=1:11
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = mean(pspt(inds{ii},is));
    end
    res_pspt(is,:) = tmp;
end

bar(res_pspt);
title('\fontsize{14}{SPT}');
ylabel('Number of Packet')
xlabel('Index of Bin')
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square


%% TLS: fig 3
fig3 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);


% TLS packet ratio
subplot_tpr = subplot(1,4,1);

tpr = X{2}(1,:)';
splits = [0.1, 0.3, 0.5, 0.7, 1/eps];
res_tpr = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(tpr(inds{ii})>=splits(is) & tpr(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_tpr(is,:) = tmp;
end

bar(res_tpr);
title('\fontsize{14}{TLS Packet Ratio}');
ylabel('Ratio')
xlabel('TLS Packet Ratio')
set(subplot_tpr, 'XTick', [1,2,3,4], 'XTickLabel', {'0.1~0.3', '0.3~0.5', '0.7~0.9', '\geq0.9'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square


% TLS version
subplot_tv = subplot(1,4,2);

tv = X{2}(2:5,:)';
res_tv = zeros(4, length(inds));
for is=1:4
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(tv(inds{ii},is))/length(inds{ii});
    end
    res_tv(is,:) = tmp;
end

bar(res_tv);
title('\fontsize{14}{TLS Version}');
ylabel('Ratio')
xlabel('TLS Version')
set(subplot_tv, 'XTick', [1,2,3,4], 'XTickLabel', {'SSL 3.0', 'TLS 1.0', 'TLS 1.1', 'TLS 1.2'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% offered compression method
subplot_tocm = subplot(1,4,3);

tocm = X{2}(347:350,:)';
res_tocm = zeros(4, length(inds));
for is=1:4
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(tocm(inds{ii},is))/length(inds{ii});
    end
    res_tocm(is,:) = tmp;
end

bar(res_tocm);
title('\fontsize{14}{Offered Compression Method}');
ylabel('Ratio')
xlabel('Index of Compression Method')
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
set(gca, 'FontSize', 12)
grid on
axis square

% selected compression method
subplot_tscm = subplot(1,4,4);

tscm = X{2}(737:740,:)';
res_tscm = zeros(4, length(inds));
for is=1:4
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(tscm(inds{ii},is))/length(inds{ii});
    end
    res_tscm(is,:) = tmp;
end

bar(res_tscm);
title('\fontsize{14}{Selected Compression Method}');
ylabel('Ratio')
xlabel('Index of Compression Method')
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
set(gca, 'FontSize', 12)
grid on
axis square

%% TLS: fig 4
fig4 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% offered extensions
subplot_toe = subplot('position', [.13  .307  .775  .42]);

toe = X{2}(351:396,:)';
res_toe = zeros(46, length(inds));
for is=1:46
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(toe(inds{ii},is))/length(inds{ii});
    end
    res_toe(is,:) = tmp;
end

bar(res_toe);
title('\fontsize{14}{Offered Extension}');
ylabel('Ratio')
xlabel('Index of Extension')
set(subplot_toe, 'XTick', [1:46])
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square

%% TLS: fig 5
fig5 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% selected extensions
subplot_tse = subplot('position', [.13  .307  .775  .42]);

tse = X{2}(741:786,:)';
res_tse = zeros(46, length(inds));
for is=1:46
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(tse(inds{ii},is))/length(inds{ii});
    end
    res_tse(is,:) = tmp;
end

bar(res_tse);
title('\fontsize{14}{Selected Extension}');
ylabel('Ratio')
xlabel('Index of Extension')
set(subplot_tse, 'XTick', [1:46])
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square


%% Cert: fig 6

fig6 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% certificate number
subplot_ccn = subplot(1,4,1);

ccn = X{3}(1,:)';
splits = [0, 2, 4, 6, 1/eps];
res_ccn = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(ccn(inds{ii})>=splits(is) & ccn(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_ccn(is,:) = tmp;
end

bar(res_ccn);
title('\fontsize{14}{Certificate Number}');
ylabel('Ratio')
xlabel('Number of Certificate')
set(subplot_ccn, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2', '2 ~ 4', '4 ~ 6', '\geq 6'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% bad certificate number
subplot_cbcn = subplot(1,4,2);

cbcn = X{3}(2,:)';
splits = [0, 2, 4, 6, 1/eps];
res_cbcn = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(cbcn(inds{ii})>=splits(is) & cbcn(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_cbcn(is,:) = tmp;
end

bar(res_cbcn);
title('\fontsize{14}{Bad Certificate Number}');
ylabel('Ratio')
xlabel('Number of Certificate')
set(subplot_cbcn, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2', '2 ~ 4', '4 ~ 6', '\geq 6'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% Certificate version ratio
subplot_cvr = subplot(1,4,3);

cvr = X{3}(3:6,:)';
res_cvr = zeros(4, length(inds));
for is=1:4
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(cvr(inds{ii},is))/length(inds{ii});
    end
    res_cvr(is,:) = tmp;
end

bar(res_cvr);
title('\fontsize{14}{Certificate Version Ratio}');
ylabel('Ratio')
xlabel('Index of Version')
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
set(gca, 'FontSize', 12)
grid on
axis square

% Certificate Validity Mean
subplot_cvm = subplot(1,4,4);

cvm = X{3}(25,:)';
splits = [0, 2000, 4000, 6000, 1/eps];
res_cvm = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(cvm(inds{ii})>=splits(is) & cvm(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_cvm(is,:) = tmp;
end

bar(res_cvm);
title('\fontsize{14}{Certificate Validity Mean}');
ylabel('Ratio')
xlabel('Validity (day)')
set(subplot_cvm, 'XTick', [1,2,3,4], 'XTickLabel', {'< 2k', '2k ~ 4k', '4k ~ 6k', '\geq 6k'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

%% Cert: fig 7
fig7 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% certificate extension ratio
subplot_ccer = subplot('position', [.13  .307  .775  .42]);

ccer = X{3}(7:24,:)';
res_ccer = zeros(18, length(inds));
for is=1:18
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = sum(ccer(inds{ii},is))/length(inds{ii});
    end
    res_cvr(is,:) = tmp;
end

bar(res_cvr);
title('\fontsize{14}{Certificate Extension Ratio}');
ylabel('Ratio')
xlabel('Index of Extension')
set(subplot_ccer, 'XTick', [1:18]);
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square


%% Cert: fig 8
fig8 = figure;
set(gcf, 'position', [0, 0, 1600, 600]);

% Certificate Key Algorithm Ratio
subplot_ckar = subplot(1,4,1);

ckar = X{3}(26:28,:)';
res_ckar = zeros(3, length(inds));
for is=1:3
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = mean(ckar(inds{ii},is));
    end
    res_ckar(is,:) = tmp;
end

bar(res_ckar);
title('\fontsize{14}{Certificate Key Algorithm Ratio}');
ylabel('Ratio')
xlabel('Index of Key Algorithm')
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square

% Certificate Signature Algorithm Ratio
subplot_csar = subplot('position',[.337  .307  .36  .42]);

csar = X{3}(29:41,:)';
res_csar = zeros(13, length(inds));
for is=1:13
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = mean(csar(inds{ii},is));
    end
    res_csar(is,:) = tmp;
end

bar(res_csar);
title('\fontsize{14}{Certificate Signature Algorithm Ratio}');
ylabel('Ratio')
xlabel('Index of Signature Algorithm')
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
% axis square


% Certificate Key Length Mean
subplot_cklm = subplot(1,4,4);

cklm = X{3}(42,:)';
splits = [0, 200, 250, 270, 1/eps];
res_cklm = zeros(length(splits)-1, length(inds));
for is=1:length(splits)-1
    split = splits(is);
    tmp = [];
    for ii=1:length(inds)
        tmp(ii) = length(find(cklm(inds{ii})>=splits(is) & cklm(inds{ii})<splits(is+1)))/length(inds{ii});
    end
    res_cklm(is,:) = tmp;
end

bar(res_cklm);
title('\fontsize{14}{Certificate Key Length Mean}');
ylabel('Ratio')
xlabel('Key Length')
set(subplot_cklm, 'XTick', [1,2,3,4], 'XTickLabel', {'<200', '200~250', '250~270', '\geq270'})
set(gca, 'FontSize', 12)
legend('Normal', 'Adw', 'Drp', 'Rtk', 'Susp');
grid on
axis square








